package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.core.domain.entity.SysRole;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.system.domain.SysPost;
import com.hxzy.system.domain.dto.SysPostSearchDTO;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.system.mapper.SysPostMapper;
import com.hxzy.system.service.ISysPostService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 岗位业务逻辑
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {
    @Override
    public IPage<SysPost> selectPostList(SysPostSearchDTO sysPostSearchDTO) {
        IPage<SysRole> page= PageDTO.of(sysPostSearchDTO.getPageNum(),sysPostSearchDTO.getPageSize());
        return  this.baseMapper.search(page,sysPostSearchDTO);
    }

    @Override
    public List<SysPost> searchExport(SysPostSearchDTO sysPostSearchDTO) {
        return this.baseMapper.searchExport(sysPostSearchDTO);
    }

    @Override
    public List<SysPost> selectPostAll() {
        return this.baseMapper.selectPostAll();
    }


    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return this.baseMapper.selectPostListByUserId(userId);
    }

    @Override
    public int checkPostNameUnique(ValueExistsDTO valueExistsDTO) {
        return this.baseMapper.checkPostNameUnique(valueExistsDTO);
    }

    @Override
    public int checkPostCodeUnique(ValueExistsDTO valueExistsDTO) {
        return this.baseMapper.checkPostCodeUnique(valueExistsDTO);
    }

    @Override
    public int countUserPostById(Long postId) {
        return this.baseMapper.countUserPostById(postId);
    }


    @Override
    public boolean deletePostByIds(Long[] postIds) {
        for (Long postId : postIds)
        {
            SysPost post = this.getById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
       return this.removeByIds( Arrays.asList(postIds));
    }


}
